package cn.csc.module.member.service.userrole;

import cn.csc.framework.common.exception.ServiceException;
import cn.csc.framework.common.pojo.PageResult;
import cn.csc.module.member.controller.app.userrole.vo.AppMemberUserRoleCreateReqVO;
import cn.csc.module.member.controller.app.userrole.vo.AppMemberUserRoleExportReqVO;
import cn.csc.module.member.controller.app.userrole.vo.AppMemberUserRolePageReqVO;
import cn.csc.module.member.controller.app.userrole.vo.AppMemberUserRoleUpdateReqVO;
import cn.csc.module.member.convert.userrole.MemberUserRoleConvert;
import cn.csc.module.member.dal.dataobject.role.MemberRoleDO;
import cn.csc.module.member.dal.dataobject.userrole.MemberUserRoleDO;
import cn.csc.module.member.dal.mysql.role.MemberRoleMapper;
import cn.csc.module.member.dal.mysql.userrole.MemberUserRoleMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.math.BigInteger;
import java.util.Collection;
import java.util.List;

import static cn.csc.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.csc.module.member.enums.ErrorCodeConstants.ROLE_CODE_NOT_EXISTS;
import static cn.csc.module.member.enums.ErrorCodeConstants.USER_ROLE_NOT_EXISTS;

/**
 * 会员和角色关联 Service 实现类
 *
 * @author liuchuang
 */
@Service
@Validated
public class MemberUserRoleServiceImpl extends ServiceImpl<MemberUserRoleMapper, MemberUserRoleDO> implements MemberUserRoleService {

    @Resource
    private MemberRoleMapper memberRoleMapper;

    @Override
    public BigInteger createUserRole(AppMemberUserRoleCreateReqVO createReqVO) {
        // 插入
        MemberUserRoleDO userRole = MemberUserRoleConvert.INSTANCE.convert(createReqVO);
        baseMapper.insert(userRole);
        // 返回
        return userRole.getId();
    }

    /**
     * 创建会员和角色关联
     *
     * @param userId 创建信息
     * @param code   角色权限字符串
     * @return 编号
     */
    @Override
    public BigInteger relate(BigInteger userId, String code) {
        MemberRoleDO memberRoleDO = memberRoleMapper.selectByCode(code);
        if (memberRoleDO == null){
            throw new ServiceException(ROLE_CODE_NOT_EXISTS);
        }
        MemberUserRoleDO build = MemberUserRoleDO.builder().userId(userId).roleId(memberRoleDO.getId()).build();
        baseMapper.insert(build);
        return build.getId();
    }

    @Override
    public void updateUserRole(AppMemberUserRoleUpdateReqVO updateReqVO) {
        // 校验存在
        validateUserRoleExists(updateReqVO.getId());
        // 更新
        MemberUserRoleDO updateObj = MemberUserRoleConvert.INSTANCE.convert(updateReqVO);
        baseMapper.updateById(updateObj);
    }

    @Override
    public void deleteUserRole(BigInteger id) {
        // 校验存在
        validateUserRoleExists(id);
        // 删除
        baseMapper.deleteById(id);
    }

    private void validateUserRoleExists(BigInteger id) {
        if (baseMapper.selectById(id) == null) {
            throw exception(USER_ROLE_NOT_EXISTS);
        }
    }

    @Override
    public MemberUserRoleDO getUserRole(BigInteger id) {
        return baseMapper.selectById(id);
    }

    @Override
    public List<MemberUserRoleDO> getUserRoleList(Collection<BigInteger> ids) {
        return baseMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<MemberUserRoleDO> getUserRolePage(AppMemberUserRolePageReqVO pageReqVO) {
        return baseMapper.selectPage(pageReqVO);
    }

    @Override
    public List<MemberUserRoleDO> getUserRoleList(AppMemberUserRoleExportReqVO exportReqVO) {
        return baseMapper.selectList(exportReqVO);
    }

}
